package com.example.demo.service;


import com.example.demo.JsonUtil;
import com.example.demo.SQLUtil;
import com.example.demo.bean.BaseResponse;
import com.example.demo.bean.ResponseDataKt;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "login", value = "/login")
public class WelcomeServlet extends HttpServlet {

    public static final String KEY_NAME = "user_name";
    public static final String KEY_PWD = "password";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println(Thread.currentThread());
        String name = request.getParameter(KEY_NAME);
        String pwd = request.getParameter(KEY_PWD);
        BaseResponse<String> responseData;
        if (SQLUtil.INSTANCE.isUser(name, pwd)) {
            responseData = new BaseResponse<>(ResponseDataKt.SUCCESS, ResponseDataKt.SUCCESS_MSG, null);
        } else {
            responseData = new BaseResponse<>(ResponseDataKt.FAILURE, ResponseDataKt.FAILURE_MSG, null);
        }
        String json = JsonUtil.INSTANCE.toJson(responseData);
        response.setContentType("text/json");
        response.getWriter().println(json);
    }

    public void destroy() {
        SQLUtil.INSTANCE.release();
    }
}